package com.question.utils;

import com.question.Vo.ChoiceVo;
import com.question.Vo.QuestionImportVo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {
    public static List<QuestionImportVo> parseExcek(MultipartFile multipartFile) throws IOException {
        List<QuestionImportVo> list = new ArrayList<>();
        //获取文件输入流
        InputStream inputStream = multipartFile.getInputStream();
        Workbook workbook = null;
        try {
            String fileName = multipartFile.getOriginalFilename();
            if (fileName != null && fileName.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(inputStream);
            } else {
                workbook = new HSSFWorkbook(inputStream);
            }
            Sheet sheet = workbook.getSheetAt(0);
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row == null) continue;
                QuestionImportVo questionImportVo = new QuestionImportVo();
                questionImportVo.setContent(getCellValue(row.getCell(0)));
                questionImportVo.setType(Integer.valueOf(getCellValue(row.getCell(1))));
                questionImportVo.setPaperid(Integer.valueOf(getCellValue(row.getCell(2))));
                if(questionImportVo.getType().equals(0) ||questionImportVo.getType().equals(1)){
                    List<ChoiceVo> list1=new ArrayList<>();
                    for (int j=0;j<4;j++){
                        ChoiceVo choiceVo=new ChoiceVo();
                        choiceVo.setContent(getCellValue(row.getCell(j+3)));
                        choiceVo.setSort(j);
                        list1.add(choiceVo);
                    }
                    questionImportVo.setChoiceVoList(list1);
                }else{
                    questionImportVo.setChoiceVoList(null);
                }
                list.add(questionImportVo);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    private static String getCellValue(Cell cell) {
        if (cell == null) {
            return null;
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue().trim();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                } else {
                    return String.valueOf((long) cell.getNumericCellValue());
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            default:
                return null;
        }
    }

    public static byte[] generateTemplate() throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("题目导入模板");
        Row headerRow = sheet.createRow(0);
        String[] headers = {
                "题目内容", "题目类型", "问卷ID", "选项A", "选项B", "选项C", "选项D"
        };
        //批量创建表头
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }
        Row exampleRow = sheet.createRow(1);
        exampleRow.createCell(0).setCellValue("在校期间的平均月消费");
        exampleRow.createCell(1).setCellValue(0);
        exampleRow.createCell(2).setCellValue(1);
        exampleRow.createCell(3).setCellValue("600-1000");
        exampleRow.createCell(4).setCellValue("1000-1500");
        exampleRow.createCell(5).setCellValue("1500-2000");
        exampleRow.createCell(6).setCellValue("2000以上");

        //自动调整列宽
        for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
        }
        try (java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream()) {
            workbook.write(out);
            workbook.close();
            return out.toByteArray();
        }
    }

}
